﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;


namespace PassBag.Logic.RecentFiles
{
    /// <summary>
    /// Help to store the recent file list
    /// </summary>
    public class RecentFileList  {
        private const int MAX_FILE = 5;

        private string[] _Items = { "", "", "", "", "" };

        /// <summary>
        /// Gets or sets the <see cref="System.String"/> at the specified index.
        /// </summary>
        /// <value>Return the file name.</value>
        public string this[int index]
        {
            get {
                if (index >= 5) {
                    throw new OverflowException(string.Format("Recent file list can only contain {0} files", MAX_FILE));
                }
                return _Items[index]; 
            }
            set {
                if (index >= 5)
                {
                    throw new OverflowException(string.Format("Recent file list can only contain {0} files", MAX_FILE));
                }

                _Items[index] = value; 
            }
        }

        /// <summary>
        /// Adds the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>True when success else false.</returns>
        public bool Add(string fileName)  {
            int findPos = Find (fileName);
            int count = Count ();

            // if empty
            if (count == 0) {
                this[0] = fileName;
                return true;
            }          
            // file already exist
            else if (findPos > 0) {
                // if is first place do nothing
                if (findPos == 1) {
                    return false;    
                }
                // swap items
                else {
                    string source = this[findPos - 1];
                    this[findPos - 1] = this[0];
                    this[0] = source;
                    return true;
                }
            }
            else if (count < MAX_FILE) {
                for (int i = MAX_FILE - 1; i > 0; i--) {
                    this[i] = this[i - 1];
                }
                this[0] = fileName;

                return true;
            }
            else {
                for (int i = MAX_FILE-1; i >0; i--) {
                    this[i] = this[i-1];
                }
                this[0] = fileName;
                return true;
            }
        }

        /// <summary>
        /// Counts this instance.
        /// </summary>
        /// <returns>The number of file.</returns>
        public int Count() {
            int count = 0;

            for (int i = 0; i < MAX_FILE; i++) {
                if (string.IsNullOrEmpty(this[count])) {
                    break;    
                }
                count++;
            }
            return count;
        }

        /// <summary>
        /// Finds the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>The position.</returns>
        public int Find (string fileName) {
            int count = 0;
            bool found = false;

            for (int i = 0; i < MAX_FILE; i++) {
                count++;

                if (this[i].Equals(fileName) ) {
                    found = true;
                    break;
                }
            }

            if (found) {
                return count;                
            }
            else {
                return 0;
            }
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear () {
            for (int i = 0; i < MAX_FILE; i++) {
                this[i] = string.Empty;
            }            
        }

    }
}
